$(document).ready(function () {

    var errorMsg = $("#errorMsg");
    var rememberMe = $("#rememberMe");
    var uName = $("#uName");
    var uPwd = $("#uPwd");
    var user;
    var friends;

    if (window.localStorage) {
        //计算缓存是否超过一周, 定时清空缓存
        var createTime = Date.parse(localStorage.getItem("createTime"));
        var newDate = new Date();
        var times = (newDate.getTime() - createTime) / (1000 * 60 * 60 * 24 * 7);
        if (times > 1) {//缓存超过一周,清空缓存
            localStorage.clear();
        }

        //获得保存的数据
        user = JSON.parse(localStorage.getItem("user"));
        friends = JSON.parse(localStorage.getItem("friends"));
        var rememberMeCache = localStorage.getItem("rememberMe");//获得之前保存的记住密码选项
        if (rememberMeCache && rememberMeCache == '1' && user != null) {//之前选择了记住密码
            uName.val(user.name);//将用户名填入表单
            uPwd.val(user.password);//将密码填入表单
            rememberMe.attr("checked", "checked");//记住密码的选择框选中
        }
    } else {
        alert('This browser does NOT support localStorage');
    }


    /**
     * 表单提交
     */
    $("#lf").submit(function (e) {
        e.preventDefault(); // prevent native submit
        //校验用户名
        if (!isUName(uName)) {
            errorMsg.html("用户名只能有数字字母组成,长度在4到16之间");
            return;
        }
        //校验密码
        if (!isPassword(uPwd)) {
            errorMsg.html("密码由数字字母标点符号组成,长度在6到32之间");
            return;
        }

        var postData = { "uName": uName.val(), "upwdMd5": uPwd.val(), "cacheMD5": localStorage.getItem("cacheMD5") }
        //提交的时候还是记住密码
        if (rememberMe.is(":checked")) {
            if (user == null || user.name != uName.val() || user.password != uPwd.val()) {//用户修改了密码,并没有使用缓存的密码
                postData.upwdMd5 = MD5(uName.val() + "-" + uPwd.val());//重新给密码加密
            }
            loginPost(postData, 1);
        } else {//没有选择记住密码
            postData.upwdMd5 = MD5(uName.val() + "-" + uPwd.val());//重新给密码加密
            loginPost(postData, 0);
        }
    });


    function loginPost(postData, rememberMe) {
        $.post("/login", postData,
            function (data) {
                //后台校验通过
                if (data.errorCode == 200) {//密码正确
                    //加上创建时间
                    localStorage.setItem("createTime", new Date());
                    //记住我 选中了
                    localStorage.setItem("rememberMe", rememberMe);
                    //存令牌
                    localStorage.setItem("token", data.token);
                    //缓存 校验不通过
                    if (data.cacheMD5 != localStorage.getItem("cacheMD5")) {//缓存用户和好友                      
                        localStorage.setItem("user", JSON.stringify(data.user));
                        localStorage.setItem("friends", JSON.stringify(data.friends));
                    }
                    //存缓存MD5码
                    localStorage.setItem("cacheMD5", data.cacheMD5);
                    //页面跳转
                    window.location.href = "gochat";
                } else if (data.errorCode == 300) {//校验没通过
                    errorMsg.html(data.msg);
                } else {
                    alert("未知错误:" + JSON.stringify(data));
                }
            }, 'json');
    }

    /**校验用户名 */
    function isUName(uName) {
        var patrn = /^[a-zA-Z0-9]{4,16}$/;//正则用于用户名的校验由数字和字母组成最少4位,最多16位
        return patrn.test(uName.val());
    }
    /**校验密码.只能由数字字母下划线和 部分标点符号组成, 最短6位, 最长20位 */
    function isPassword(uPwd) {
        var patrn = /^[a-zA-Z0-9_!@#\$%\^&\*\(\)\+=\-`~.<>/?|\\]{6,32}$/;
        return patrn.test(uPwd.val());
    }

})